{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "ec2ef1fd-e51c-4b89-aad3-793f438af0c2",
   "metadata": {},
   "source": [
    "<center><img src=\"./image/CLBLOGO.jpg\" alt=\"创乐博\" style=\"width: 300px;\"/></center>\n",
    "\n",
    "# 35.RFID 射频卡模块实验\n",
    "\n",
    "@－－－－湖南创乐博智能科技有限公司－－－－<br>\n",
    "@  文件名：35.RFID射频卡模块实验.py <br>\n",
    "@  版本：V2.0 <br>\n",
    "@  author: zhulin<br>\n",
    "@  说明：RFID 射频卡模块实验\n",
    "我们来学习一下摄像头的开启方式之一吧，一般来说，对于我们使用摄像头的时候，首先我们需要测试我们的摄像头是否正常工作，画面是否清晰，让我们一起来测试我们的摄像头吧！！！<br>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ef02f441-ac93-4c66-bd55-ad21ea265d3d",
   "metadata": {},
   "source": [
    "## 1.导入必要的库文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "44f1253a-efdb-4425-933b-eecd0b602437",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "from gpiozero import LED\n",
    "import serial"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "34b5a1dd-afc0-4164-93c8-5763c73abb60",
   "metadata": {},
   "source": [
    "## 2.定义使能管脚和串口端口号"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e0055d6c-68f8-4a8f-8ba0-d646ff211992",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "ENABLE_PIN  = 17              # ENABLE Pin GPIO17\n",
    "# Sout Pin - Raspberry Pi 5 Uart RXI \n",
    "SERIAL_PORT = '/dev/ttyAMA0'  # The location of our serial port. \n",
    "   \n",
    "\n",
    "ENABLE = LED(ENABLE_PIN)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8601d653-1d4c-431a-8bd2-81785e9acaf8",
   "metadata": {},
   "source": [
    "## 3.定义功能函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9b92a3d6-277d-4714-b6c5-39135cdbda39",
   "metadata": {},
   "outputs": [],
   "source": [
    "def validate_rfid(code):\n",
    "    # A valid code will be 12 characters long with the first char being\n",
    "    # a line feed and the last c har being a carriage return.\n",
    "    s = code.decode(\"ascii\")\n",
    "    if (len(s) == 12) and (s[0] == \"\\n\") and (s[11] == \"\\r\"):\n",
    "        return s[1:-1]\n",
    "    else:\n",
    "        # We didn't match a valid code, so return False.\n",
    "        return False"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4dd6afca-d816-4210-95ba-0e7b3aa70040",
   "metadata": {},
   "source": [
    "## 4.定义主函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1cd28813-3537-4110-90ee-61ce7182a38f",
   "metadata": {},
   "outputs": [],
   "source": [
    "def main():\n",
    "    print(\"Enabling RFID reader...\\n\")\n",
    "    ENABLE.off()  # 使能ENABLE\n",
    "    # Set up the serial port as per the makerobo reader's datasheet.\n",
    "    ser = serial.Serial(baudrate = 2400,\n",
    "                        bytesize = serial.EIGHTBITS,\n",
    "                        parity   = serial.PARITY_NONE,\n",
    "                        port     = SERIAL_PORT,\n",
    "                        stopbits = serial.STOPBITS_ONE,\n",
    "                        timeout  = 1)\n",
    "    \n",
    "    # Wrap everything in a try block to catch any exceptions.\n",
    "    try:\n",
    "        # Loop forever, or until CTRL-C is pressed.\n",
    "    \n",
    "        while 1:\n",
    "            # Read in 12 bytes from the serial port.\n",
    "            data = ser.read(12)\n",
    "            # Attempt to validate the data we just read.\n",
    "            code = validate_rfid(data)\n",
    "    \n",
    "            # If validate_rfid() returned a code, display it.\n",
    "            if code:\n",
    "                print(\"Read RFID code: \" + code);\n",
    "    except Exception:\n",
    "        # If we caught an exception, then disable the reader by setting\n",
    "        # the pin to HIGH, then exit.\n",
    "        print (\"Couldn't do it: %s\" % e)\n",
    "        print(\"Disabling RFID reader...\")\n",
    "        ENABLE.on()  # 关闭ENABLE\n",
    "\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    main()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
